package com.example.maybeaccount.Mode;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.maybeaccount.ActivityShowDetails;
import com.example.maybeaccount.MainActivity;
import com.example.maybeaccount.R;

import java.util.List;

/**
 * 账单适配器，用于RecyclerView显示
 */
public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ViewHolder>{
    private List<Item> itemList;
    /**
     *  用于选择适配器的模式   0 - 年，1 - 月，2 - 账户
     */
    private int mode;
    private Context context;
    public static final int YEAR_MODE = 0;
    public static final int MONTH_MODE = 1;
    public static final int ACCOUNT_MODE = 2;
    public static final int ALL_MODE = 3;
    public static final int CREDIT_MODE = 4;
    public static final int CHILD_MODE = 5;

    static class ViewHolder extends RecyclerView.ViewHolder {
        View itemView;
        TextView amountText;
        TextView timeText;
        TextView expendText;
        TextView incomeText;
        TextView yearOfMonthText;
        TextView tipsIncome;
        TextView tipsExpend;
        TextView tipsBalance;
        ImageButton detailButton;
        RecyclerView recyclerViewInside;
        boolean isOpen;

        public ViewHolder(View view) {
            super(view);
            itemView = view;
            amountText = view.findViewById(R.id.amount);
            timeText = view.findViewById(R.id.month);
            expendText = view.findViewById(R.id.expend);
            incomeText = view.findViewById(R.id.income);
            yearOfMonthText = view.findViewById(R.id.year);
            tipsIncome = view.findViewById(R.id.tipsIncome);
            tipsExpend = view.findViewById(R.id.tipsExpend);
            tipsBalance = view.findViewById(R.id.tips_balance);
            detailButton = view.findViewById(R.id.details);
            recyclerViewInside = view.findViewById(R.id.recycler_view_inside);
            isOpen = false;
        }
    }

    /**
     * 此处设置点击响应
     * @param parent
     * @param viewType
     * @return
     */
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view;
        if(mode == CHILD_MODE){
            view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.frame_count_child, parent, false);
        }else{
            view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.frame_count, parent, false);
        }

        final ViewHolder holder = new ViewHolder(view);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int position = holder.getAdapterPosition();
                Item item = itemList.get(position);

            }
        });
        /** 点击展开按钮 */
        holder.detailButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int position = holder.getAdapterPosition();
                Item item = itemList.get(position);
                if(mode == ALL_MODE || mode == CREDIT_MODE || mode == CHILD_MODE){
                    Intent intent = new Intent(context, ActivityShowDetails.class);
                    //发送记账类别
                    intent.putExtra("mode",item.getMode());
                    intent.putExtra("item",item);
                    intent.putExtra("activity",((Activity)context).getLocalClassName());
                    context.startActivity(intent);
                    //context.startActivity(intent);
                }
                /** 展开某月的全部信息 */
                if(mode == MONTH_MODE){
                    if(holder.isOpen){
                        holder.isOpen = false;
                        holder.recyclerViewInside.setVisibility(View.GONE);
                        holder.recyclerViewInside.addItemDecoration(new SpacesItemDecoration(-2));
                    }else{
                        holder.isOpen = true;
                        List<Item> childItemList = ((MainActivity)context).itemManager.getItemsOfMonth(item.getYear(),item.getMonth());
                        ItemAdapter itemAdapter = new ItemAdapter(childItemList,ItemAdapter.CHILD_MODE,context);
                        LinearLayoutManager layoutManager= new LinearLayoutManager(context);
                        holder.recyclerViewInside.setLayoutManager(layoutManager);
                        holder.recyclerViewInside.setAdapter(itemAdapter);
                        //设置各个item之间的间隔
                        int space = 2;
                        holder.recyclerViewInside.addItemDecoration(new SpacesItemDecoration(space));
                        holder.recyclerViewInside.setVisibility(View.VISIBLE);
                    }
                }
            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Item item = itemList.get(position);
        holder.tipsExpend.setText("支出");
        holder.tipsExpend.setTextColor(Color.parseColor("#43CD80"));
        holder.tipsIncome.setText("收入");
        holder.tipsIncome.setTextColor(Color.parseColor("#CD5C5C"));
        holder.tipsBalance.setText("结余");
        holder.detailButton.setVisibility(View.INVISIBLE);
        if(mode == YEAR_MODE){
            holder.amountText.setText(item.getAmount()+"");
            holder.timeText.setText(item.getStringYear());
            holder.yearOfMonthText.setText("年");
            holder.expendText.setText(item.getStringExpend());
            holder.incomeText.setText(item.getStringIncome());
        }else if(mode == MONTH_MODE){
            holder.amountText.setText(item.getAmount()+"");
            holder.timeText.setText(item.getStringMonth()+"月");
            holder.expendText.setText(item.getStringExpend());
            holder.incomeText.setText(item.getStringIncome());
            holder.yearOfMonthText.setText(item.getStringYear());
            holder.detailButton.setVisibility(View.VISIBLE);
        }else if(mode == ACCOUNT_MODE){
            holder.amountText.setText(item.getAmount()+"");
            holder.timeText.setText(item.getAccount());
            holder.yearOfMonthText.setText("账户");
            holder.expendText.setText(item.getStringExpend());
            holder.incomeText.setText(item.getStringIncome());
        }else if(mode == ALL_MODE || mode == CHILD_MODE){
            holder.amountText.setText(item.getAmount()+"");
            holder.timeText.setText(item.getStringMonth()+"月"+item.getStringDay()+"日");
            holder.yearOfMonthText.setText(item.getStringYear());
            holder.expendText.setText(item.getAccount());
            holder.incomeText.setText("账户：");
            holder.detailButton.setVisibility(View.VISIBLE);
            holder.tipsExpend.setVisibility(View.INVISIBLE);
            holder.tipsIncome.setVisibility(View.INVISIBLE);
            holder.tipsBalance.setText(item.getClass1()+(item.getClass2() == null?"":"-"+item.getClass2()));
        }else if(mode == CREDIT_MODE){
            holder.amountText.setText(Math.abs(item.getAmount())+"");
            holder.timeText.setText(item.getStringMonth()+"月"+item.getStringDay()+"日");
            holder.yearOfMonthText.setText(item.getStringYear());
            holder.tipsIncome.setText("还款日");
            holder.incomeText.setText(item.getStringPayBackYear());
            holder.tipsExpend.setText("   ");
            holder.tipsBalance.setText(item.getAccount());
            holder.expendText.setText(item.getStringPayBackTimeWithoutYear());
            holder.detailButton.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public int getItemCount() {
        return itemList.size();
    }

    public ItemAdapter(List<Item> itemList, int mode, Context context) {
        this.mode = mode;
        this.context = context;
        this.itemList = itemList;
    }

}
